import {signup} from "../../lib/auth";
import {setUser} from "../../store/user";

Page({
    data: {
        email: '',
        username: '',
        password: '',
        error: '',
        disable: false,
    },
    // 清空表单
    clearForm() {
        this.setData({
            email: '',
            username: '',
            password: '',
        })
    },
    setDisable(disable) {
        this.setData({disable})
    },
    setError(error) {
        this.setData({error})
    },
    inputEmail(e) {
        this.setData({
            email: e.detail.value
        })
    },
    inputUsername(e) {
        this.setData({
            username: e.detail.value
        })
    },
    inputPassword(e) {
        this.setData({
            password: e.detail.value
        })
    },
    submit(e) {
        this.setDisable(true)
        signup(this.data.email, this.data.username, this.data.password,
            (res) => {
                if (res.statusCode === 500) {
                    this.setError('服务端错误')
                    this.setDisable(false)
                    return
                }
                // 记录登录信息
                setUser(res.data.user)

                // 清空输入框
                this.clearForm();
                this.setDisable(false)
                this.setError(undefined)
                // 跳转index
                // navigateTo:fail can not navigateTo a tabbar page
                // wx.navigateTo({url: '../index/index'})
                // wx.reLaunch({url: '../index/index'})
                wx.switchTab({url: '../index/index'})
            },
        )
    },
    toSignin(e) {
        wx.reLaunch({
            url: '../login/login',
        })
    }
    ,
    onLoad(options) {

    }
    ,
    onReady() {

    }
    ,
    onShow() {

    }
    ,
    onHide() {

    }
    ,
    onUnload() {

    }
    ,
    onShareAppMessage() {
        return {
            title: '',
        };
    }
    ,
});